package com.biz.primus.app.tms.config;

import com.biz.primus.base.global.Ref;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.sql.Timestamp;

import static springfox.documentation.schema.Annotations.findPropertyAnnotation;

/**
 * @author yanweijin
 * @date 2017/9/13
 */
@Configuration
@EnableSwagger2
@Profile({"!release", "!production", "!master"})
public class Swagger2Configuration {

    @Value("${spring.application.name}")
    private String serviceName;

    private String serviceVersion = "1.0.0";

    @Bean
    Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.biz.primus"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(Timestamp.class, Long.class);
    }

    @Bean
    ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("api docs : service-" + serviceName).version(serviceVersion).build();
    }


    @Bean
    @Order(Ordered.LOWEST_PRECEDENCE)
    ModelPropertyBuilderPlugin customModelPropertyPlugin(){
        return new ModelPropertyBuilderPlugin(){

            @Override
            public boolean supports(DocumentationType delimiter) {
                return DocumentationType.SWAGGER_2==delimiter;
            }

            @Override
            public void apply(ModelPropertyContext context) {
                Optional<Ref> ref = findRef(context);
                if (ref.isPresent()) {
                    context.getBuilder().isHidden(true);
                }
            }
            private Optional<Ref> findRef(ModelPropertyContext context){
                Optional<Ref> annotation = Optional.absent();

                if (context.getAnnotatedElement().isPresent()) {
                    Ref ref = AnnotationUtils.findAnnotation(context.getAnnotatedElement().get(), Ref.class);
                    annotation = annotation.or(Optional.fromNullable(ref));
                }
                if (context.getBeanPropertyDefinition().isPresent()) {
                    annotation = annotation.or(findPropertyAnnotation(context.getBeanPropertyDefinition().get(), Ref.class));
                }
                return annotation;
            }
        };
    }
}
